package com.ruibang.glass.produce.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.common.constants.RedisKey;
import com.ruibang.glass.produce.constant.Constants;
import com.ruibang.glass.produce.domain.req.ElasticModulusReq;
import com.ruibang.glass.produce.entity.ElasticModulus;
import com.ruibang.glass.produce.mapper.ElasticModulusMapper;
import com.ruibang.glass.produce.service.ElasticModulusService;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 生产-理化-弹性模量 服务实现类
 * </p>
 *
 * @author songJinKang
 * @since 2024-03-13
 */
@Service
public class ElasticModulusServiceImpl extends ServiceImpl<ElasticModulusMapper, ElasticModulus> implements ElasticModulusService {

    @Autowired
    private RedisSingleService redisService;


    @Override
    public void delete(String id) {
        lambdaUpdate().eq(ElasticModulus::getId, id).set(ElasticModulus::getIsDelete, Constants.ONE_STR).update();
    }

    @Override
    public IPage<ElasticModulus> pageQuery(ElasticModulusReq elasticModulusReq) {
        QueryWrapper<ElasticModulus> qw = new QueryWrapper<>();
        qw.lambda().eq(ElasticModulus::getIsDelete, Constants.ZERO_STR);
        qw.lambda().like(StringUtils.isNotBlank(elasticModulusReq.getCheckBy()), ElasticModulus::getCheckBy, elasticModulusReq.getCheckBy());
        qw.lambda().like(StringUtils.isNotBlank(elasticModulusReq.getCode()), ElasticModulus::getCode, elasticModulusReq.getCode());
        if (elasticModulusReq.getStartTime() != null && elasticModulusReq.getEndTime() != null) {
            qw.lambda().ge(ElasticModulus::getCreateTime, elasticModulusReq.getStartTime());
            qw.lambda().le(ElasticModulus::getCreateTime, elasticModulusReq.getEndTime());
        }
        return this.page(new Page<>(elasticModulusReq.getPageNo(), elasticModulusReq.getPageSize()), qw.lambda());
    }

    @Override
    public void saveInfo(ElasticModulus elasticModulus) {
        elasticModulus.setIsDelete(Constants.ZERO_STR);
        String checkById = elasticModulus.getCheckById();
        if (StringUtils.isNotBlank(checkById)) {
            Object userObj = redisService.hget(RedisKey.USER_GROUP_TYPE, checkById);
            if (userObj != null && StringUtils.isNotBlank(userObj.toString())) {
                elasticModulus.setCheckBy(userObj.toString());
            }
        }
        this.save(elasticModulus);
    }

    @Override
    public void edit(ElasticModulus elasticModulus) {
        String checkById = elasticModulus.getCheckById();
        if (StringUtils.isNotBlank(checkById)) {
            Object userObj = redisService.hget(RedisKey.USER_GROUP_TYPE, checkById);
            if (userObj != null && StringUtils.isNotBlank(userObj.toString())) {
                elasticModulus.setCheckBy(userObj.toString());
            }
        } else {
            elasticModulus.setCheckBy("");
        }
        this.updateById(elasticModulus);
    }
}
